
class BattleBehaviour extends Behaviour {

    $getDamage = 0;
    $isAttacked = true;
    $word;

    tern = 'hero';

    effect;
    clearButton;
    hero;
    monster;


    onStart() {


        this.effect = core.getObjectById('effect');
        this.clearButton = core.getObjectById('clearButton');
        this.hero = core.getObjectById('hero').getBehaviour(Character);
        this.monster = core.getObjectById('monster').getBehaviour(Character);


    }

    onUpdate(duringTime) {



        if (this.monster.hpNow <= 0) {

            this.$word = 'hero打败了monster！';
            this.tern = 'end';

        } else if (this.hero.hpNow <= 0) {

            this.$word = 'monster打败了hero！';
            this.tern = 'end';

        }
        if ((this.hero.hpNow <= 0 || this.monster.hpNow <= 0) && this.clearButton.visible == false) {

            this.effect.visible = true;
            this.effect.getBehaviour(WordRenderer).word = this.$word;

            this.clearButton.visible = false;
            this.clearButton.getBehaviour(ClearButton).interactable = false;

            for (const button of core.getObjectById("button").children) {
                button.visible = false;
            }


            console.log('heroHP:  ' + this.hero.hpNow);
            console.log('monsterHP:  ' + this.monster.hpNow);
        }

        if (this.hero.hpNow > 0 && this.monster.hpNow > 0) {

            if (this.tern == 'hero') {

                this.$isAttacked = false;
            } else if (this.tern == 'monster') {

                this.effect.visible = true;
                this.effect.getBehaviour(WordRenderer).word = 'monster攻击了hero！';


                if (this.$isAttacked == false) {
                    this.$getDamage = 2020;
                    this.$isAttacked = true;
                }


                this.clearButton.visible = true;
                this.clearButton.getBehaviour(ClearButton).interactable = true;
                // }, 1000);

            }

            if (this.$getDamage > 0) {
                if (this.$getDamage >= 50) {
                    this.hero.hpNow -= 50;
                    //console.log('this.heroHP after attacked:  ' + this.heroHP.hpNow);
                    this.$getDamage -= 50;
                    //console.log('getDamage last:  ' + this.$getDamage);
                } else {
                    this.hero.hpNow -= this.$getDamage;
                    if (this.hero.hpNow < 0) {
                        this.hero.hpNow = 0;
                    }
                    this.$getDamage = 0;

                    console.log('heroHP after attacked:  ' + this.hero.hpNow);
                }

            }
        }
    }
}

class SkillButton extends Button {

    //canClick = true;
    $isClicked = false;
    $damage;
    skill;
    //testDamage = 0;

    effect;
    clearButton;
    hero;
    monster;
    tern;


    onAddToGameObject() {
        this.gameObject.renderer = this;
    }

    onStart() {

        this.effect = core.getObjectById('effect');
        this.clearButton = core.getObjectById('clearButton');
        this.hero = core.getObjectById('hero').getBehaviour(Character);
        this.monster = core.getObjectById('monster').getBehaviour(Character);
        this.tern = core.getObjectById('gameRule').getBehaviour(BattleBehaviour).tern;

        this.onClick = () => {
            //console.log(this.gameObject);



            if (this.tern == 'hero') {


                for (const button of core.getObjectById("button").children) {
                    button.visible = false;
                }


                this.effect.visible = true;
                this.effect.getBehaviour(WordRenderer).word = 'hero使用了' + this.word + '！';
                this.clearButton.visible = true;
                this.clearButton.getBehaviour(ClearButton).interactable = true;
                this.$damage = this.skill.calculateDamage(this.hero, this.monster);
                this.$damage = parseInt(this.$damage);
                this.$isClicked = true;
                console.log('hero damage done:   ' + this.$damage);
            }

        }
    }

    onUpdate(duringTime) {

        this.interactable = this.gameObject.visible;


        if (this.$isClicked && this.monster.hpNow > 0) {
            //console.log('onUpdate');

            if (this.$damage >= 50) {
                this.monster.hpNow -= 50;
                this.$damage -= 50;

                //this.testDamage += 10;
            } else {
                this.monster.hpNow -= this.$damage;
                //this.testDamage += this.damage;
                this.$damage = 0;
            }

            if (this.$damage == 0) {
                //console.log('test:' + this.testDamage);
                //this.gameObject.removeBehaviour(this);
                //this.monster.hpNow = this.monsterHP.hpNow;
                if (this.monster.hpNow < 0) {
                    this.monster.hpNow = 0;
                }
                console.log('final monsterHP:     ' + this.monster.hpNow);
                this.$isClicked = false;
            }
        } else if (this.monster.hpNow == 0) {
            this.gameObject.removeBehaviour(this);
            this.$isClicked = false;
        }

    }


    // parseFromData(data) {
    //     super.parseFromData(data);
    //     if (data.skill) this.skill = data.skill;
    // }

    // toData() {
    //     return {
    //         word: this.word,
    //         color: this.color,
    //         length: this.length,
    //         width: this.width,
    //         interactable: this.interactable,
    //         skill: this.skill
    //     }
    // }
}

class ClearButton extends Button {

    effect;
    clearButton;
    tern;

    onAddToGameObject() {
        this.gameObject.renderer = this;
    }

    onStart() {

        this.tern = core.getObjectById('gameRule').getBehaviour(BattleBehaviour).tern;
        this.effect = core.getObjectById('effect');
        this.clearButton = core.getObjectById('clearButton');

        this.clearButton.visible = false;
        this.clearButton.getBehaviour(ClearButton).interactable = false;



        this.onClick = () => {
            //console.log(this.gameObject);


            if (this.tern == 'hero') {

                //getDamage = 2000;
                this.tern = 'monster';
                core.getObjectById('gameRule').getBehaviour(BattleBehaviour).tern = 'monster';
            } else if (this.tern == 'monster') {
                this.tern = 'hero';
                core.getObjectById('gameRule').getBehaviour(BattleBehaviour).tern = 'hero';


                for (const button of core.getObjectById("button").children) {
                    button.visible = true;
                }

            }


            this.effect.visible = false;
            this.clearButton.visible = false;
            this.clearButton.getBehaviour(ClearButton).interactable = false;

        }
    }


}

class MedicalButton extends Button {


    $isClicked = false;
    $totalHealth = 0;

    @SerializedField(0)
    ammount = 0;


    effect;
    clearButton;
    hero;
    monster;


    onStart() {

        this.gameObject.renderer = this;

        this.effect = core.getObjectById('effect');
        this.clearButton = core.getObjectById('clearButton');
        this.hero = core.getObjectById('hero').getBehaviour(Character);
        this.monster = core.getObjectById('monster').getBehaviour(Character);


        this.onClick = () => {


            for (const button of core.getObjectById("button").children) {
                button.visible = false;
            }


            this.effect.visible = true;
            if (this.ammount > 0) {
                this.effect.getBehaviour(WordRenderer).word = 'hero使用了' + this.word + '！';
                this.$totalHealth = 3000;
                this.$isClicked = true;
            } else {
                this.effect.getBehaviour(WordRenderer).word = 'hero的' + this.word + '用光了！';
            }

            this.clearButton.visible = true;
            this.clearButton.getBehaviour(ClearButton).interactable = true;

        }
    }

    onUpdate(duringTime) {


        if (this.$isClicked) {


            if (this.hero.hpNow < this.hero.hpMax && this.$totalHealth > 0) {

                if (this.$totalHealth >= 50) {
                    this.$totalHealth -= 50;
                    this.hero.hpNow += 50;
                } else {
                    this.hero.hpNow += this.$totalHealth;
                    this.$totalHealth = 0;
                }


            }
            if (this.hero.hpNow >= this.hero.hpMax) {
                this.hero.hpNow = this.hero.hpMax;
                this.hero.hpNow = this.hero.hpMax;
                this.$totalHealth = 0;
            }



            if (this.$totalHealth == 0) {
                this.ammount -= 1;
                this.$isClicked = false;

                console.log('final hp:  ' + this.hero.hpNow);
            }
            if (this.ammount == 0) {
                //this.gameObject.removeBehaviour(this);
            }
        }
    }

    // parseFromData(data) {
    //     super.parseFromData(data);
    //     this.ammount = isNaN(data.ammount) ? 0 : data.ammount;
    // }

    // toData() {
    //     return {
    //         word: this.word,
    //         color: this.color,
    //         length: this.length,
    //         width: this.width,
    //         interactable: this.interactable,
    //         ammount: this.ammount
    //     }
    // }
}

class SkillSquareRenderer extends Behaviour {


    onAddToGameObject() {
        this.gameObject.renderer = this;
    }

    onDraw(context) {

        context.save();

        context.beginPath();
        context.fillStyle = 'gray';
        context.fillRect(0, 250, 400, 150);
        context.closePath();
        context.fill();

        context.beginPath();
        context.moveTo(0, 250);
        context.lineTo(400, 250);
        context.moveTo(395, 250);
        context.lineTo(395, 400);
        context.moveTo(395, 395);
        context.lineTo(0, 395);
        context.moveTo(5, 395);
        context.lineTo(5, 250);
        context.closePath();
        const gradient = context.createLinearGradient(0, 250, 400, 400);
        gradient.addColorStop("0", "magenta");
        gradient.addColorStop("0.5", "blue");
        gradient.addColorStop("1.0", "red");
        context.strokeStyle = gradient;
        context.lineWidth = 7;
        context.stroke();

        context.beginPath();
        context.moveTo(270, 270);
        context.lineTo(270, 380);
        context.closePath();
        context.lineWidth = 4;
        context.stroke();

        context.restore();
    }

    drawSquare(xPosition, yPosition, color, length, lineWidth) {
        context.strokeStyle = color;
        context.beginPath();
        context.rect(xPosition, yPosition, length, length);
        context.closePath();
        context.lineWidth = lineWidth;
        context.stroke();
    }

    hitTest(point) {

    }
}

class Skill extends Behaviour {

    @SerializedField('')
    num = '';
    @SerializedField('')
    skillName = '';
    @SerializedField('')
    skillType = '';
    @SerializedField('')
    calculator = '';

    onStart() {

    }

    calculateDamage(P1, P2) {
        let P1MainPower = 0;
        let P2MainPower = 0;
        if (this.skillType == 'AD') {
            P1MainPower = P1.strong;
            P2MainPower = P2.strong;
        } else if (this.skillType == 'AP') {
            P1MainPower = P1.intelligence;
            P2MainPower = P2.intelligence;
        }
        const percent = (100 + (10 - (Math.random() * 20))) / 100;
        const damage = eval(this.calculator) * percent;
        return damage;
    }

    // parseFromData(data) {
    //     super.parseFromData(data);
    //     this.num = data.num;
    //     this.skillName = data.skillName;
    //     this.skillType = data.skillType;
    //     this.calculator = data.calculator;
    // }
}

core.registerBehaviourClass(BattleBehaviour);
core.registerBehaviourClass(SkillButton);
core.registerBehaviourClass(ClearButton);
core.registerBehaviourClass(MedicalButton);
core.registerBehaviourClass(SkillSquareRenderer);
core.registerBehaviourClass(Skill);

//core.registerBehaviourClass(AddSkillButtonPerfabBehaviour);
